package com.warmlight.voicepacket.widget;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.adapter.RecommendRVAdapter;
import com.warmlight.voicepacket.data.RecommendData;

import java.util.List;

/**
 * Created by jason on 2018/5/22.
 */

public class FooterListView extends ListView {

    private Context mContext;
    private View mFooter;
    private RecommendHeader mHeader;
    private TextView tv_footer_hint,tv_footer_click;

    public FooterListView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public FooterListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public FooterListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public FooterListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        init();
    }

    /**
     * 控件处理类型
     */
    public enum ActionResult {
        /**
         * 下拉刷新完毕
         */
        REFRESH_COMPLETED,

        /**
         * 还有更多数据
         */
        HAS_MORE,

        /**
         * 没有更多数据
         */
        NO_MORE,

        /**
         * 加载中
         */
        LOADING,

        /**
         * 什么也不显示
         */
//        HIDE,

        /**
         * 加载出错
         */
        ERROR
    }

    private void init() {
        mFooter = View.inflate(mContext, R.layout.footer_lv, null);
        this.addFooterView(mFooter);
        tv_footer_click = (TextView) mFooter.findViewById(R.id.tv_footer_click);
        tv_footer_click.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onFooterClicked();
                }
            }
        });
        tv_footer_click.setVisibility(View.GONE);
        tv_footer_hint = (TextView) mFooter.findViewById(R.id.tv_footer_hint);
        tv_footer_hint.setVisibility(View.VISIBLE);

        addFooterAction();
    }

    //临时添加此方法，用于填充
    public void setRecommendHeaderData(List<RecommendData.DataBean.PlayCoversBean> list, String category_id){
        if(mHeader != null){
            mHeader.setCategoryId(category_id);
            if(list != null && list.size()>0){
//                addRecommendHeader(mContext);
                mHeader.getAdapter().addList(list);
            }else{

            }
        }
    }

    public List<RecommendData.DataBean.PlayCoversBean> getRecommendHeaderData(){
        if (mHeader != null){
            return mHeader.getAdapter().getList();
        }
        return null;
    }

    public RecommendRVAdapter getRVAdapter(){
        if (mHeader != null){
            return mHeader.getAdapter();
        }
        return null;
    }

    //临时添加此方法，只给1天时间没办法哈
    public void setBannerData(List<RecommendData.DataBean.BannerBean> list){
        if(mHeader != null){
            if(list != null && list.size()>0){
                mHeader.setBannerVisible(true);
//                mHeader.getBannerAdapter().setList(list);
                ((BannerTopic)(mHeader.findViewById(R.id.bn_recommend))).setData(list);
            }else{
                mHeader.setBannerVisible(false);
            }
        }
    }

    public void setF4(List<RecommendData.DataBean.ToolBean> tool) {
        if (mHeader != null){
             if (tool != null && tool.size() == 4){
                 mHeader.setF4(tool);
             }
        }
    }



    //设置滑动监听
    private boolean isLastItemVisiable = false;
    private void addFooterAction(){
        this.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(0 == scrollState && isLastItemVisiable && mLastItemVisiableListener!=null&&!isFooterLoading()
                        &&!"已显示全部".equals(tv_footer_hint.getText().toString())){
                    mLastItemVisiableListener.onLastItemVisiable();
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int lastItem = firstVisibleItem + visibleItemCount;
//				 Log.e("WZY", "firstVisibleItem= "+firstVisibleItem);
//				 Log.e("WZY", "visibleItemCount= "+visibleItemCount);
//				 Log.e("WZY", "lastItem= "+lastItem);
//				 Log.e("WZY", "totalItemCount= "+totalItemCount);
                if(lastItem == totalItemCount) {
//					 View lastItemView=(View) listView.getChildAt(listView.getChildCount()-1);
//					 if ((listView.getBottom())==lastItemView.getBottom()) {
//						 System.out.println("==Scroll to the listview bottom ==");
//					 }
                    isLastItemVisiable = true;
                }else{
                    isLastItemVisiable = false;
                }
            }
        });
    }

    private LastItemVisiableListener mLastItemVisiableListener;
    public interface LastItemVisiableListener{
        /**
         * 当footer loading时，不会重复调用此方法
         */
        void onLastItemVisiable();
    }
    public void setOnLastItemVisiableListener(LastItemVisiableListener listener){
        mLastItemVisiableListener = listener;
    }

    //设定添加recommendHeader
    public void addRecommendHeader(){
        if (mHeader == null) {
            mHeader = new RecommendHeader(mContext);
            this.addHeaderView(mHeader);
        }
    }

    private FooterClickListener mListener;
    public interface FooterClickListener{
        void onFooterClicked();
    }
    public void setOnFooterClickListener(FooterClickListener listener){
        mListener = listener;
    }

    /**
     * 控制footer结果
     * @param result 要显示的结果
     */
    public void setResult(ActionResult result) {
        switch (result) {
            case REFRESH_COMPLETED:
                break;
            case HAS_MORE:
                tv_footer_click.setVisibility(View.GONE);
                tv_footer_hint.setVisibility(View.VISIBLE);
                tv_footer_hint.setText("上滑加载更多");
                break;
            case NO_MORE:
                tv_footer_click.setVisibility(View.GONE);
                tv_footer_hint.setVisibility(View.VISIBLE);
                tv_footer_hint.setText("已显示全部");
                break;
            case ERROR:
                tv_footer_click.setVisibility(View.VISIBLE);
                tv_footer_hint.setVisibility(View.GONE);
                break;
            case LOADING:
                tv_footer_click.setVisibility(View.GONE);
                tv_footer_hint.setVisibility(View.GONE);
                break;
//            case HIDE:
//                tv_footer_click.setVisibility(View.GONE);
//                tv_footer_hint.setVisibility(View.GONE);
//                break;

            default:
                break;
        }
    }

    /**
     * 判断控件底部是否正在加载更多
     * @return true,if its loading,otherwise.
     */
    public boolean isFooterLoading() {
        return (View.GONE == tv_footer_click.getVisibility() && View.GONE == tv_footer_hint.getVisibility());
            //当存在footer且两个提示都不显示的时候，证明正在loading
    }

    public View getFooterView(){
        return mFooter;
    }
}
